<?php
// Copyright (C) 2010 Rod Roark <rod@sunsetsystems.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

require_once("../../globals.php");
require_once("$srcdir/api.inc");
require_once("$srcdir/forms.inc");
require_once("$srcdir/options.inc.php");
require_once("$srcdir/formdata.inc.php");
require_once("$srcdir/formatting.inc.php");

// Defaults for new orders.
$row = array(
    'provider_id' => $_SESSION['authUserID'],
    'date_ordered' => date('Y-m-d'),
    'date_collected' => date('Y-m-d H:i'),
);

if (!$encounter) { // comes from globals.php
    die("Internal error: we do not seem to be in an encounter!");
}

function cbvalue($cbname) {
    return $_POST[$cbname] ? '1' : '0';
}

function cbinput($name, $colname) {
    global $row;
    $ret = "<input type='checkbox' name='$name' value='1'";
    if ($row[$colname])
        $ret .= " checked";
    $ret .= " />";
    return $ret;
}

function cbcell($name, $desc, $colname) {
    return "<td width='25%' nowrap>" . cbinput($name, $colname) . "$desc</td>\n";
}

function QuotedOrNull($fld) {
    if (empty($fld))
        return "NULL";
    return "'$fld'";
}

$formid = formData('id', 'G') + 0;

// If Save was clicked, save the info.
//
if ($_POST['bn_save']) {
    // If updating an existing form...
    //
        if ($formid) {
        $sets =
                "procedure_type_id = " . (formData('form_proc_type') + 0) . ", " .
                "date_ordered = " . QuotedOrNull(formData('form_date_ordered')) . ", " .
                "provider_id = " . (formData('form_provider_id') + 0) . ", " .
                "date_collected = " . QuotedOrNull(formData('form_date_collected')) . ", " .
                "order_priority = '" . formData('form_order_priority') . "', " .
                "order_status = '" . formData('form_order_status') . "', " .
                "patient_instructions = '" . formData('form_patient_instructions') . "', " .
                "patient_id = '" . $pid . "', " .
                "encounter_id = '" . $encounter . "'";
        $query = "UPDATE procedure_order SET $sets " .
                "WHERE procedure_order_id = '$formid'";
        sqlStatement($query);
    }
    // If adding a new form...
    //
        else {
        $contador = 0;
        while ($_POST['form_proc_type' . $contador]) {
            $sets =
                    "procedure_type_id = " . (formData('form_proc_type' . $contador) + 0) . ", " .
                    "date_ordered = " . QuotedOrNull(formData('form_date_ordered')) . ", " .
                    "provider_id = " . (formData('form_provider_id') + 0) . ", " .
                    "date_collected = " . QuotedOrNull(formData('form_date_collected')) . ", " .
                    "order_priority = '" . formData('form_order_priority') . "', " .
                    "order_status = '" . formData('form_order_status') . "', " .
                    "patient_instructions = '" . formData('form_patient_instructions') . "', " .
                    "patient_id = '" . $pid . "', " .
                    "encounter_id = '" . $encounter . "'";

            $query = "INSERT INTO procedure_order SET $sets";
            $newid = sqlInsert($query);
            addForm($encounter, "Procedure Order", $newid, "procedure_order", $pid, $userauthorized);

            $contador++;
        }
    }
    ?>
    <html>
        <head>
            <?php html_header_show(); ?>
            <title><?php xl('Delete Patient, Encounter, Form, Issue, Document, Payment, Billing or Transaction', 'e'); ?></title>
            <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
            <link rel="stylesheet" href="css/nightingale.css" type="text/css"/>

            <script language='JavaScript'>
                setTimeout(function() {
                    parent.$.fancybox.close();
                    $("#procedimientos_lista").html("");
                }, 1000);
            </script>
        </head>

        <body class="body_top">
        <center>
            <p class="text">
                <?php echo "Guardado con &eacute;xito"; ?>  
            </p>
            <p class="text">
                Este dialogo se cerrar&aacute; autom&aacute;ticamente...
            </p>
        </center>
    </body>
    </html>
    <?php
//    formHeader("Redirecting....");
//    formJump();
//    formFooter();
    exit;
}

if ($formid) {
    $row = sqlQuery("SELECT * FROM procedure_order WHERE " .
            "procedure_order_id = '$formid' AND activity = '1'");
}

$enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, fe.date FROM " .
        "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
        "p.pid = '$pid' AND f.pid = '$pid' AND f.encounter = '$encounter' AND " .
        "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
        "fe.id = f.form_id LIMIT 1");
?>
<html>
    <head>
        <?php html_header_show(); ?>
        <link rel="stylesheet" href="<?php echo $css_header; ?>" type="text/css" />

        <style>

            td {
                font-size:10pt;
            }

            .inputtext {
                padding-left:2px;
                padding-right:2px;
            }

        </style>

            <!--<link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/interface/themes/nightingale.css" >-->
            <!--<link rel="stylesheet" href="<?php echo $GLOBALS['webroot'] ?>/library/js/redmond/jquery-ui-1.10.3.custom.min.css" type="text/css"/>-->
            <!--<link rel="stylesheet" href="<?php echo $GLOBALS['webroot'] ?>/patients/css/datepickerAppointment.css" type="text/css"/>-->

            <!--<style type="text/css">@import url(<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.css);</style>-->
            <!--<script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.js"></script>-->
        <?php // include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php");  ?>
            <!--<script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_setup.js"></script>-->

        <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
        <!--<script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/textformat.js"></script>-->

        <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/patients/js/jquery.ui.core.js"></script>
        <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/patients/js/jquery.ui.widget.js"></script>
        <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/patients/js/jquery.ui.datepicker.js"></script>
        <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/patients/js/jquery.mask.min.js"></script>

        <script language='JavaScript'>

            // This invokes the find-procedure-type popup.
            var ptvarname;
            function sel_proc_type(varname) {
                var f = document.forms[0];
                if (typeof varname == 'undefined')
                    varname = 'form_proc_type';
                ptvarname = varname;

                dlgopen('../../orders/types.php?popup=1&order=' + f[ptvarname].value, '_blank', 800, 500);
            }

            // This is for callback by the find-procedure-type popup.
            // Sets both the selected type ID and its descriptive name.
            function set_proc_type(typeid, typename) {
                var f = document.forms[0];
                f[ptvarname].value = typeid;
                f[ptvarname + '_desc'].value = typename;
            }

            function set_procedimiento(name) {
                $("#procedimientos_lista").html(name);
            }

        </script>

    </head>

    <body class="body_top">
        <div class="border">
            <form method="post" action="<?php echo $rootdir ?>/forms/procedure_order/new.php?id=<?php echo $formid ?>" id="procedimiento_form">

                <center>

                    <p>
                    <table border='0' width='95%'>

                        <?php
                        $ptid = -1; // -1 means no order is selected yet
                        $ptrow = array('name' => '');
                        if (!empty($row['procedure_type_id'])) {
                            $ptid = $row['procedure_type_id'];
                            $ptrow = sqlQuery("SELECT name FROM procedure_type WHERE " .
                                    "procedure_type_id = '$ptid'");
                        }
                        ?>
                        <tr>
                            <td colspan="3">
                                <h2 class='title' style=''>
                                    <?php
                                    echo xl('Procedure Order') . ' ';
                                    //echo $enrow['fname'] . ' ' . $enrow['mname'] . ' ' . $enrow['lname'];
                                    echo oeFormatShortDate(substr($enrow['date'], 0, 10));
                                    ?>

                                </h2>
                            </td>
                        </tr>
                        <tr>
                            <td width='1%' nowrap><b>Procedimientos:</b></td>
                            <td>
                                <div id="procedimientos_lista" style="margin-top: 2px; margin-bottom: 2px"><?php echo addslashes($ptrow['name']) ?></div>
<!--                                <input type='text' size='50' name='form_proc_type_desc'
                                       value='<?php echo addslashes($ptrow['name']) ?>'
                                       style='width:100%;cursor:pointer;cursor:hand' readonly />-->
                                   <!--  <input type='text' size='50' name='form_proc_type_desc'
                                       value='<?php echo addslashes($ptrow['name']) ?>'
                                       onclick='sel_proc_type()' onfocus='this.blur()'
                                       title='<?php xl('Click to select the desired procedure', 'e'); ?>'
                                       style='width:100%;cursor:pointer;cursor:hand' readonly />-->
                                <input type='hidden' name='form_proc_type' value='<?php echo $ptid ?>' />

                            </td>
                            <td>
                                <?php
                                if (!$formid) {
                                    ?>
                                    <a href="<?php echo $rootdir ?>/orders/types.php?popup=1&order=-1" class="boton_normal" id="selProc">Selecccionar</a> 
                                    <?php
                                }
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td width='1%' nowrap><b><?php xl('Ordering Provider', 'e'); ?>:</b></td>
                            <td colspan="2">
                                <?php
                                generate_form_field(array('data_type' => 10, 'field_id' => 'provider_id'), $row['provider_id']);
                                ?>
                            </td>
                        </tr>

                        <tr>
                            <td width='1%' nowrap><b><?php xl('Date Ordered', 'e'); ?>:</b></td>
                            <td colspan="2">
                                <?php
                                echo "<input type='text' size='10' name='form_date_ordered' id='form_date_ordered'" .
                                " value='" . $row['date_ordered'] . "'" .
                                " title='" . xl('Date of this order') . "'" .
                                " class='my_datepicker'" .
                                " />";
                                ?>
                            </td>
                        </tr>

                        <tr>
                            <td width='1%' nowrap><b><?php xl('Internal Time Collected', 'e'); ?>:</b></td>
                            <td colspan="2">
                                <?php
                                echo "<input type='text' size='16' name='form_date_collected' id='form_date_collected'" .
                                " value='" . $row['date_collected'] . "'" .
                                " title='" . xl('Date and time that the sample was collected') . "'" .
                                " class='my_datepicker'" .
                                " />";
                                ?>
                            </td>
                        </tr>

                        <tr>
                            <td width='1%' nowrap><b><?php xl('Priority', 'e'); ?>:</b></td>
                            <td colspan="2">
                                <?php
                                generate_form_field(array('data_type' => 1, 'field_id' => 'order_priority',
                                    'list_id' => 'ord_priority'), $row['order_priority']);
                                ?>
                            </td>
                        </tr>

                        <tr>
                            <td width='1%' nowrap><b><?php xl('Status', 'e'); ?>:</b></td>
                            <td colspan="2">
                                <?php
                                generate_form_field(array('data_type' => 1, 'field_id' => 'order_status',
                                    'list_id' => 'ord_status'), $row['order_status']);
                                ?>
                            </td>
                        </tr>

                        <tr>
                            <td width='1%' nowrap><b><?php xl('Patient Instructions', 'e'); ?>:</b></td>
                            <td colspan="2">
                                <textarea rows='3' cols='40' name='form_patient_instructions' style='width:100%'
                                          wrap='virtual' class='inputtext' /><?php echo $row['patient_instructions'] ?></textarea>
                            </td>
                        </tr>

                    </table>

                    <p>
                        <button type='submit' class="boton_normal" onclick="return validar();"><i class="icon-save"></i> <?php xl('Save', 'e'); ?></button>
                        <input type="hidden" name='bn_save' value="1" />
                        &nbsp;
                        <button type='button' class="boton_normal" onclick="javascript:back();"><i class="icon-undo"></i> <?php xl('Back', 'e'); ?></button>
                    </p>
                    <br/>
                </center>

                <script language='JavaScript'>
            function validar() {
                if( $("#procedimientos_lista").html() === ""){
                    alert("Escoja los procedimientos")
                    return false;
                }
                return true;
            }
            
            function back() {
                $("#column_left").load("<?php echo $GLOBALS['webroot'] ?>/interface/patient_file/encounter/forms.php?set_encounter=<?php echo $_REQUEST["set_encounter"]; ?>");
            }

            $(document).ready(function() {

                $("#procedimiento_form").bind("submit", function() {

                    $.ajax({
                        type: "POST",
                        cache: false,
                        url: "<?php echo $rootdir ?>/forms/procedure_order/new.php?id=<?php echo $formid ?>",
                        data: $(this).serializeArray(),
                        success: function(data) {
                            $.fancybox(data);
                        }
                    });

                    return false;
                });

                $('#selProc').click(function(e) {
                    e.preventDefault();
                    parent.$.fancybox({
                        href: this.href,
                        autoSize: true,
                        type: 'iframe',
                        helpers: {
                            overlay: {
                                opacity: 0.3,
                                css: {
                                    'background': 'rgba(255, 254, 254,0.85)'
                                }
                            } // overlay
                        }
                    }); // fancybox
                }); // click

                $("#form_date_ordered").datepicker({
                    showOn: "button",
                    buttonImage: "<?php echo $GLOBALS['webroot'] ?>/patients/images/calendar-green.gif",
                    dateFormat: 'yy-mm-dd',
                    buttonImageOnly: true
                });
                $("#form_date_ordered").mask("9999-99-99");
                $("#form_date_collected").datepicker({
                    showOn: "button",
                    buttonImage: "<?php echo $GLOBALS['webroot'] ?>/patients/images/calendar-green.gif",
                    dateFormat: 'yy-mm-dd',
                    buttonImageOnly: true
                });
                $("#form_date_collected").mask("9999-99-99");
            });
                </script>

            </form>
        </div>
    </body>
</html>

